package com.bennu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bennu.Dto.SetmealDto;
import com.bennu.common.CustomException;
import com.bennu.domain.Setmeal;
import com.bennu.domain.SetmealDish;
import com.bennu.mapper.SetmealMapper;
import com.bennu.service.SetmealDishService;
import com.bennu.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j

public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    //新增套餐，同时保存套餐和菜品关系
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存菜品基本信息,操作setmeal，执行insert操作
        this.save( setmealDto );
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //获取SetmealDish中的setmealid（套餐id）将setmealDt中id赋值给SetmealDish中的setmealid
        setmealDishes=setmealDishes.stream().map( (item)->{
            item.setSetmealId( setmealDto.getId() );
            return item;
        } ).collect( Collectors.toList() );
        //保存关联信息，操作setmeal_dish，执行insert操作
        setmealDishService.saveBatch( setmealDishes );

    }
    //删除套餐
    @Override
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态是否起售,确定是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.in( Setmeal::getId,ids );
        queryWrapper.eq( Setmeal::getStatus,1 );
        int count = (int) this.count( queryWrapper );
        //如果不能删除抛出异常
        if ( count>0 ){
            //状态为1在起售不能删除
            throw new CustomException( "当前状态不能删除" );
        }
        //如果可以删除，先删除套餐表中的数据
        this.removeByIds( ids );
        //删除关系表中的数据setmeal_dish
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in( SetmealDish::getId,ids );
        //删除关系表中的菜品
        setmealDishService.remove( setmealDishLambdaQueryWrapper );
    }
    //数据回显根据id查询
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //查询套餐基本信息
        Setmeal setmeal = this.getById( id );
        //拷贝
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties( setmeal,setmealDto );
        //查询套餐菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq( SetmealDish::getSetmealId,setmeal.getId() );
        List<SetmealDish> list = setmealDishService.list( queryWrapper );
        setmealDto.setSetmealDishes( list );
        return setmealDto;
    }
    //修改套餐
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //更新setmeal表数据
       this.updateById( setmealDto );
        //清理当前菜品对应口味数据setmealDish数据，delete操作
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq( SetmealDish::getId,setmealDto.getId() );
        setmealDishService.remove( queryWrapper );
        //添加提交过来的餐品数据setmealDish，insert操作
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map( (item)->{
            item.setSetmealId( setmealDto.getId() );
            return item;
        } ).collect( Collectors.toList() );
        setmealDishService.saveBatch( setmealDishes );
    }


}
